<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MedicalComplaint extends Model
{
    protected $table = 'st_medical_complaints';

    public $timestamps = false;

    protected $fillable = [
        'record_id',
        'symptoms',
        'blood_glucose',
        'weightchange',
        'weightchangestatus',
        'weightchangekg',
        'medication',
        'cn_medication',
        'remark',
    ];

    protected $casts = [
        'symptoms'      => 'array',
        'blood_glucose' => 'array',
        'medication'    => 'array',
        'cn_medication' => 'array',
    ];

    public function medicalRecord()
    {
        return $this->belongsTo(MedicalRecord::class, 'record_id');
    }

    public function getWeightchangeAttribute($value)
    {
        $field = app('field')->get('weight');

        return ['key' => $value, 'value' => isset($field['change'][$value]) ? $field['change'][$value] : ''];
    }

    public function getWeightchangestatusAttribute($value)
    {
        $field = app('field')->get('weight');

        return ['key' => $value, 'value' => isset($field['status'][$value]) ? $field['status'][$value] : ''];
    }

    public function setWeightchangekgAttribute($value)
    {
        $status = app('request')->input('weightchangestatus') ?: $this->attributes['weightchangestatus'];

        if ($status != 1) {
            $this->attributes['weightchangekg'] = 0;
        } else {
            $this->attributes['weightchangekg'] = $value;
        }
    }
}
